/***************************************************************************
 * *   File name: ClientManager.cpp
 * *    Describe: 客户端的管理
 * *  Created on: 2016-8-6
 * *      Author: dengxiudong
 * ************************************************************************/ 
#include "ClientManager.h"

ClientManager * ClientManager::instance()
{
	static ClientManager obj;
	return &obj;
	/*if (obj == nullptr)
	{
		obj = new ClientManager();
	}

	return obj;*/
}

Client * ClientManager::getClient(uint32_t clientId)
{
	m_mutex.lock();
	std::unordered_map<uint32_t, Client*>::iterator it = m_cliMap.find(clientId);
	m_mutex.unlock();
	if (it != m_cliMap.end())
	{
		return it->second;
	}
	else
	{
		return nullptr;
	}
}

void ClientManager::addClient(uint32_t clientId, Client * ptrCli)
{
	m_mutex.lock();
	m_cliMap[clientId] = ptrCli;
	m_mutex.unlock();
}

bool ClientManager::delClient(uint32_t clientId, Client *ptrCli)
{
	m_mutex.lock();
	auto it = m_cliMap.find(clientId);
	if (it != m_cliMap.end() && it->second == ptrCli)
	{
		m_cliMap.erase(it);
		m_mutex.unlock();
		return true;
	}
	else
	{
		m_mutex.unlock();
		return false;
	}
}

bool ClientManager::isExist(uint32_t clientId)
{
	std::lock_guard<std::mutex> locker(m_mutex);
	auto it = m_cliMap.find(clientId);
	if (it != m_cliMap.end())
	{
		return true;
	}
	return false;
}

